package com.wht.media.config;

import com.wht.media.common.R;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Managed;
import org.noear.solon.core.exception.StatusException;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;
import org.noear.solon.core.route.RouterInterceptor;
import org.noear.solon.core.route.RouterInterceptorChain;

/**
 * @author by HaiTao.Wang on 2025/9/12.
 */
@Slf4j
@Managed
public class AppRouterInterceptor implements RouterInterceptor {

    @Override
    public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {

        try {
            chain.doIntercept(ctx, mainHandler);
        } catch (StatusException e) {
            ctx.render(R.error(e.getCode(), e.getMessage()));
        } catch (Throwable e) {
            log.error(e.getMessage(), e);
            ctx.render(R.error(500, e.getMessage()));
        }
    }

}
